package com.example.activiti.controller;

import com.example.activiti.common.MessageBody;
import com.example.activiti.config.UserLocal;
import com.example.activiti.model.ActUser;
import com.example.activiti.model.LeaveDTO;
import com.example.activiti.service.ProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/activiti")
public class ActivityModelController {


    private final ProcessService processService;

    public ActivityModelController(ProcessService processService) {
        this.processService = processService;
    }


    /**
     * 流程定义列表
     */
    @RequestMapping(value = "/process-list")
    public MessageBody processList() {
        return MessageBody.success(processService.findProcessList());
    }

    /**
     * 新建一个空模型
     */
    @RequestMapping("/process-create")
    public MessageBody newModel() {
        return MessageBody.success(processService.createProcessModel());
    }

    /**
     * 部署流程模型
     */
    @RequestMapping("/deploy-model")
    public MessageBody deploy(@RequestParam("modelId") String modelId) throws Exception {
        processService.deployModel(modelId);
        return MessageBody.success();
    }

    /**
     * 部署流程资源
     */
    @RequestMapping(value = "/process-upload")
    public MessageBody deploy(@RequestParam("file") MultipartFile file) throws Exception {
        processService.processUpload(file);
        return MessageBody.success();
    }

    /**
     * 动态表单/外置表单提交任务
     *
     * @param param
     * @return java.lang.String
     * @author Mr.yuan
     * @date 2019/11/12 13:48
     **/
    @PostMapping(value = "/process-start")
    public MessageBody startProcessInstance(@RequestBody LeaveDTO param) {

        // 获取当前登录的用户
        ActUser user = UserLocal.getUser();
        // 用户未登录不能操作，实际应用使用权限框架实现，例如Spring Security、Shiro等
        if (user == null || Objects.isNull(user.getId())) {
            return MessageBody.login();
        }
        param.setApplyUserId(user.getId());
        processService.submitProcess(param);
        return MessageBody.success();
    }

    /**
     * 获取请假类型
     */
    @RequestMapping("/getLeaveType")
    public MessageBody getLeaveType() {
        return MessageBody.success(processService.getLeaveType());
    }

    /**
     * 挂起任务
     */
    @RequestMapping("/process-suspend")
    public MessageBody suspendTask(@RequestParam("processInstanceId") String processInstanceId) {
        processService.suspendTask(processInstanceId);
        return MessageBody.success();
    }

    /**
     * 激活任务
     */
    @RequestMapping("/process-active")
    public MessageBody activeTask(@RequestParam("processInstanceId") String processInstanceId) {
        processService.activeTask(processInstanceId);
        return MessageBody.success();
    }

    /**
     * 签收任务
     */
    @RequestMapping("/process-claim")
    public MessageBody claim(String taskId) {
        processService.claim(taskId);
        return MessageBody.success();
    }

    /**
     * 查找全部办理任务
     */
    @RequestMapping("/process-model-list")
    public MessageBody findModelList() {
        return MessageBody.success(processService.findModelList());
    }

    /**
     * 查找全部办理任务
     */
    @RequestMapping("/process-handle-tasks")
    public MessageBody findMyTasks() {
        return MessageBody.success(processService.findHandleTasks(UserLocal.getUser().getId()));
    }

    /**
     * 查找全部我的任务
     */
    @RequestMapping("/process-organizer-tasks")
    public MessageBody findOrganizerTasks() {
        return MessageBody.success(processService.findOrganizerTasks(UserLocal.getUser().getId()));
    }

    /**
     * 查找代办任务
     */
    @RequestMapping("/process-candidate-tasks")
    public MessageBody findCanTasks() {
        return MessageBody.success(processService.findCandidateTasks(UserLocal.getUser().getId()));
    }

    /**
     * 查找已办任务
     */
    @RequestMapping("/process-history-tasks")
    public Object findHistoryTasks() {
        return MessageBody.success(processService.findHistoryTasks(UserLocal.getUser().getId()));
    }

    /**
     * 任务跟踪
     */
    @RequestMapping(value = "/process-task-trace")
    public void readResource(String processInstanceId, HttpServletResponse response) throws IOException {
        processService.taskTrace(processInstanceId, response);
    }

    /**
     * 读取流程资源
     *
     * @param processDefinitionId 流程定义ID
     * @param resourceName        资源名称
     */
    @RequestMapping(value = "/process-view")
    public MessageBody readResource(@RequestParam("processDefinitionId") String processDefinitionId, @RequestParam("resourceName") String resourceName, HttpServletResponse response)
            throws Exception {
        return processService.processView(processDefinitionId,resourceName,response);
    }
    /**
     * 导出流程bpmn和png
     */
    @RequestMapping("/export")
    public MessageBody export(@RequestParam("deploymentId") String deploymentId) throws IOException {
        processService.export(deploymentId);
        return MessageBody.success();
    }

}
